﻿using System;
using System.Windows.Forms;
using CQRS.Events;
using CQRS.Sample.Domain.Commands;

namespace CQRS.Example.SampleApplication.Views
{
    public partial class RegisterCrashForm : BaseForm
    {
        private readonly IBus<Command> _commandBus;
        private Guid _planeId;

        public RegisterCrashForm(IBus<Command> commandBus)
        {
            InitializeComponent();
            _commandBus = commandBus;
        }

        private void buttonRegisterPlaneCrash_Click(object sender, EventArgs e)
        {
            int aboard, fatalities;
            if (!ParseInt(textBoxAboard, out aboard))
            {
                MessageBox.Show("Cannot register the crash, cannot parse the number of people aboard.");
                return;
            }
            if (!ParseInt(textBoxFatalities, out fatalities))
            {
                MessageBox.Show("Cannot register the crash, cannot parse the number of fatalities.");
                return;
            }
            var registerPlaneCrashCommand = new RegisterPlaneCrashCommand(_planeId, textBoxLocation.Text, aboard,
                                                                          fatalities, textBoxRoute.Text,
                                                                          dateTimePickerCrashDate.Value);
            _commandBus.Publish(registerPlaneCrashCommand);
            _commandBus.Commit();
            Close();
        }

        internal void Init(Guid planeId)
        {
            _planeId = planeId;
        }
    }
}